package com.ruoyi.project.exam.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.IdGen;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.exam.domain.ExamModel;
import com.ruoyi.project.exam.domain.ExamPaperModel;
import com.ruoyi.project.exam.domain.PaperModel;
import com.ruoyi.project.exam.domain.RepoModel;
import com.ruoyi.project.exam.domain.UserExamModel;
import com.ruoyi.project.exam.service.IExamPaperService;
import com.ruoyi.project.exam.service.IExamService;
import com.ruoyi.project.exam.service.IPaperService;

/**
 * 在线开始
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/exam/examOnline")
public class ExamOnlineController extends BaseController{
	
	private String prefix = "exam/examOnline";
	
	@Autowired
	private IExamService examService;
	@Autowired
	private IExamPaperService examPaperService;
	@Autowired
	private IPaperService paperService;
	
	@RequiresPermissions("exam:examOnline:view")
	@GetMapping()
	public String examOnline(ModelMap mmap) {
		return prefix + "/examOnline";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("exam:examOnline:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ExamModel model) {
		startPage();
		List<ExamModel> list = examService.selectExamOnlineList(model);
		return getDataTable(list);
	}
	
	/**
	 * 考试准备
	 */
	@GetMapping("/examPre/{id}")
	public String examPre(@PathVariable("id") String id, ModelMap mmap) {
		Long userId = ShiroUtils.getUserId();
		ExamModel model = examService.selectUserExam(id, userId);
		mmap.put("model", model);
		return prefix + "/examPre";
	}
	
	/**
	 * 创建考试试卷
	 */
	@GetMapping("/createPaper/{id}")
	public String createPaper(@PathVariable("id") String id, ModelMap mmap) {
		ExamPaperModel model = examPaperService.selectExamPaperRandom(id);
		mmap.put("model", model);
		return prefix + "/paper";
	}
	
	/**
	 * 查询考试试卷
	 */
	@PostMapping("/selectPaperDetail")
	@ResponseBody
	public AjaxResult selectPaperDetail(String id) {
		PaperModel model = paperService.selectPaperDetail(id);
		return AjaxResult.success(model);
	}
	/**
	 * 提交试卷
	 * @param paper
	 * @return
	 */
	@PostMapping("/addPaper")
	@ResponseBody
	public AjaxResult addPaper(String paper) {
		try{
			if(StringUtils.isNotBlank(paper)){
				PaperModel model = JSON.parseObject(paper,  PaperModel.class);
				String paperId = IdGen.uuid();
				model.setId(paperId);
				Map<String, Object> map = paperService.addPaper(model);
				return AjaxResult.success(map);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return AjaxResult.error();
	}
	/**
	 * 考试结果
	 * @param id
	 * @param mmap
	 * @return
	 */
	
	@GetMapping("/paperResult/{id}")
	public String paperResult(@PathVariable("id") String id, ModelMap mmap) {
		PaperModel model = paperService.selectUserExamPaper(id);
		mmap.put("model", model);
		return prefix + "/result";
	}
}
